package com.dfdchem.imd.kaiwu.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {

    private static final String TAG = "RegexUtil";

    public static boolean isPhone(String phone) {
        String regExp = "^1[3|4|5|6|7|8|9]\\d{9}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phone);
        return m.find();
    }

    private static final String LinkRegex = "(?<!href=\")(http|https|arouter)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";

    // 给消息内容的普通链接 比如 http://www.baidu.com arouter://kaiwu/kaiwu/env_setting 等添加点击事件
    public static String contentClickable(String content){
        if (content == null || content.equals("")) return null;
        Pattern pattern = Pattern.compile(LinkRegex);
        Matcher matcher = pattern.matcher(content);

        StringBuffer sb=new StringBuffer();

        while (matcher.find()) {
            String s = matcher.group();
            matcher.appendReplacement(sb, "<a href=\""+s+"\">"+s+"</a>");
        }
        matcher.appendTail(sb);
        String result = sb.toString();
        result = result.replace("\r\n", "<br>");
        result = result.replace("\n", "<br>");
        return result;
    }
}